<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$merchid=$this->checklogin();
$tabs=array(
	array('op'=>'main','title'=>'基本设置'),
	array('op'=>'feelist','title'=>'满额优惠列表'),
	array('op'=>'feeadd','title'=>'添加满额优惠'),
	array('op'=>'numlist','title'=>'满件优惠列表'),
	array('op'=>'numadd','title'=>'添加满件优惠'),
	array('op'=>'hunlist','title'=>'混批折扣列表'),
	array('op'=>'hunadd','title'=>'添加混批折扣'),
);
$return['status']=array('未启用','已启用');
$return['types']=array('立减','打折');
$return['huntypes']=array('1'=>'订单总金额达满额条件','2'=>'订单内指定商品总额达满额条件','3'=>'订单内单品金额各达满额条件');
$return['goodtype']=array('0'=>'不添加商品限制','1'=>'允许以下商品使用');
$return['catetype']=array('0'=>'不添加商品分类限制','1'=>'允许以下商品分类使用');
$return['hungoodtype']=array('0'=>'不添加商品独立折扣','1'=>'允许以下商品独立折扣');
$return['huncatetype']=array('0'=>'不添加商品分类独立折扣','1'=>'允许以下商品分类独立折扣');
$return['islimitlevel']=array('0'=>'不添加会员等级限制','1'=>'允许以下会员等级使用');
if($_RQ['op']=='main'){
	$item=$this->plugin_setting('saleset'.$merchid);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'满额/件设置',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'switch','name'=>'enough_open','title'=>'是否开启满额/件优惠','help'=>'这里是总开关'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='feelist' || $_RQ['op']=='numlist' || $_RQ['op']=='hunlist'){
	$saleset=$this->plugin_setting('saleset'.$merchid);
	if($_RQ['op']=='feelist'){
		$list=$saleset['enough_feeset'];
		$edit_op='feeadd';
		$del_op='delfee';
	}elseif($_RQ['op']=='numlist'){
		$list=$saleset['enough_numset'];
		$edit_op='numadd';
		$del_op='delnum';
	}else{
		$list=$saleset['enough_hunset'];
		$return['types']=$return['huntypes'];
		$edit_op='hunadd';
		$del_op='delhun';
		$return['goodtype']=$return['hungoodtype'];
		$return['catetype']=$return['huncatetype'];
	}
	foreach($list as $k=>&$lv){
		$lv['id']=$lv['id']?:$k+1;
	}
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、<red>这里可以设置，不同商品，不同分类下的满优惠规则！</red>
		；
		','edit_op'=>$edit_op,'del_op'=>$del_op,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('type'=>'string','name'=>'title','title'=>'优惠标题'),
			array('name'=>'type','title'=>'优惠类型','type'=>'radio','options'=>'types'),
			array('name'=>'goodtype','title'=>'商品限制','type'=>'radio','options'=>'goodtype'),
			array('name'=>'catetype','title'=>'分类限制','type'=>'radio','options'=>'catetype'),
			array('name'=>'status','title'=>'是否开启','type'=>'radio','options'=>'status'),
		),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='feeadd' || $_RQ['op']=='numadd' || $_RQ['op']=='hunadd'){
	$saleset=$this->plugin_setting('saleset'.$merchid);
	$id=$_RQ['id'];
	if($_RQ['op']=='feeadd'){
		$set=$saleset['enough_feeset'];
		$topost='postfee';
	}elseif($_RQ['op']=='numadd'){
		$set=$saleset['enough_numset'];
		$topost='postnum';
	}else{
		$set=$saleset['enough_hunset'];
		$topost='posthun';
	}
	if($id){
		$k=$id-1;
		foreach($set as $k2=>$sv){
			if($sv['id']==$id || $k==$k2){
				$item=$sv;
			}
		}
	}
	$item['id']=$id?:'';
	if($_RQ['op']!='hunadd'){
		$setgroupids=array();
		$groupids=str_array($item['groupids']);
		foreach($groupids as $cid){
			$pcate=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$cid,'uniacid'=>$_SESSION['uniacid']),'parentid');
			if(!$pcate){
				$pcate=$cid;
				$cid=0;
			}
			$setgroupids[]=array('pcate'=>$pcate,'ccate'=>$cid);
		}
		$item['setgroupids']=$setgroupids;
		$setgoodsids=array();
		$goodids=str_array($item['goodids']);
		foreach($goodids as $gid){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$gid,'uniacid'=>$_SESSION['uniacid']),array('pcate2','ccate2'));
			$setgoodsids[]=array('goodsid'=>$gid,'pcate'=>$goods['pcate2'],'ccate'=>$goods['ccate2']);
		}
		$item['setgoodsids']=$setgoodsids;
	}
	$item['status']=$item['status']?:0;
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>$topost,
		'desc'=>'满优惠设置',
		'tabs'=>$tabs,
		'edit'=>array()
	);
	if($_RQ['op']=='feeadd'){
		$htmls['edit']=array(
					array('type'=>'switch','name'=>'status','title'=>'是否开启'),
					array('type'=>'string','name'=>'title','title'=>'优惠标题'),
					array('type'=>'radio','name'=>'type','title'=>'优惠类型','options'=>'types'),	
					array('type'=>'array','name'=>'set1','title'=>'满额立减','add_tip'=>'添加一个优惠项','show'=>array('type'=>0),
						'list'=>array(
							array('type'=>'unitstring','unit'=>'元','name'=>'man','title'=>'满'),	
							array('type'=>'unitstring','unit'=>'元','name'=>'jian','title'=>'减'),	
						),
						'details'=>array(
							array('type'=>'unitstring','unit'=>'元','name'=>'man','title'=>'满'),	
							array('type'=>'unitstring','unit'=>'元','name'=>'jian','title'=>'减'),
						)
					),
					array('type'=>'array','name'=>'set2','title'=>'满额打折','add_tip'=>'添加一个打折项','show'=>array('type'=>1),
						'list'=>array(
							array('type'=>'unitstring','unit'=>'元','name'=>'man','title'=>'满'),	
							array('type'=>'unitstring','unit'=>'折','name'=>'rate','title'=>'打N折'),	
						),
						'details'=>array(
							array('type'=>'unitstring','unit'=>'元','name'=>'man','title'=>'满'),	
							array('type'=>'unitstring','unit'=>'折','name'=>'rate','title'=>'打N折','help'=>'填写：1~10的数字；例如：填8，表示打8折'),
						)
					),
					array('type'=>'radio','name'=>'catetype','options'=>'catetype','title'=>'分类使用限制'),	
					array('type'=>'array','show'=>array('catetype'=>1),'name'=>'setgroupids','title'=>'选择商品分类','add_tip'=>'添加一个分类',
						'list'=>array(
							array('type'=>'select','name'=>'pcate','options'=>'allgroups','title'=>'一级分类'),
							array('type'=>'select','name'=>'ccate','options'=>'allgroups','title'=>'二级分类'),
						),
						'details'=>array(
							array('type'=>'select2_line','name'=>'pcate','options'=>'pcates','title'=>'选择分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'二级分类',
							'name3'=>'scate','options3'=>'scates','title3'=>'三级分类',
							),
						)
					),
					array('type'=>'radio','name'=>'goodtype','options'=>'goodtype','title'=>'商品使用限制'),
					array('type'=>'array','show'=>array('goodtype'=>1),'name'=>'setgoodsids','title'=>'选择商品','add_tip'=>'添加一个商品',
						'list'=>array(
							array('type'=>'select','name'=>'pcate','options'=>'allgroups','title'=>'一级分类'),
							array('type'=>'select','name'=>'ccate','options'=>'allgroups','title'=>'二级分类'),
							array('type'=>'select','name'=>'goodsid','options'=>'goods','title'=>'商品名称'),
						),
						'details'=>array(
							array('type'=>'select3_line','name'=>'pcate','options'=>'pcategoods','title'=>'选择分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'二级分类',
							'name3'=>'goodsid','options3'=>'goods','title3'=>'请选择商品',
							),
						)
					),
					array('type'=>'radio','name'=>'islimitlevel','options'=>'islimitlevel','title'=>'会员使用限制'),
					array('type'=>'checkbox','name'=>'memberlevels','options'=>'member','title'=>'选择会员等级','show'=>array('islimitlevel'=>1)),
			);
	}elseif($_RQ['op']=='numadd'){
		$htmls['edit']=array(
					array('type'=>'switch','name'=>'status','title'=>'是否开启'),
					array('type'=>'string','name'=>'title','title'=>'优惠标题'),
					array('type'=>'radio','name'=>'type','title'=>'优惠类型','options'=>'types'),	
					array('type'=>'array','name'=>'set1','title'=>'满件立减','add_tip'=>'添加一个优惠项','show'=>array('type'=>0),
						'list'=>array(
							array('type'=>'unitstring','unit'=>'件','name'=>'man','title'=>'满'),	
							array('type'=>'unitstring','unit'=>'元','name'=>'jian','title'=>'减'),	
						),
						'details'=>array(
							array('type'=>'unitstring','unit'=>'件','name'=>'man','title'=>'满'),	
							array('type'=>'unitstring','unit'=>'元','name'=>'jian','title'=>'减'),
						)
					),
					array('type'=>'array','name'=>'set2','title'=>'满件打折','add_tip'=>'添加一个打折项','show'=>array('type'=>1),
						'list'=>array(
							array('type'=>'unitstring','unit'=>'件','name'=>'man','title'=>'满'),	
							array('type'=>'unitstring','unit'=>'折','name'=>'rate','title'=>'打N折'),	
						),
						'details'=>array(
							array('type'=>'unitstring','unit'=>'件','name'=>'man','title'=>'满'),	
							array('type'=>'unitstring','unit'=>'折','name'=>'rate','title'=>'打N折','help'=>'填写：1~10的数字；例如：填8，表示打8折'),
						)
					),
					array('type'=>'radio','name'=>'catetype','options'=>'catetype','title'=>'分类使用限制'),	
					array('type'=>'array','show'=>array('catetype'=>1),'name'=>'setgroupids','title'=>'选择商品分类','add_tip'=>'添加一个分类',
						'list'=>array(
							array('type'=>'select','name'=>'pcate','options'=>'allgroups','title'=>'一级分类'),
							array('type'=>'select','name'=>'ccate','options'=>'allgroups','title'=>'二级分类'),
						),
						'details'=>array(
							array('type'=>'select2_line','name'=>'pcate','options'=>'pcates','title'=>'选择分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'二级分类',
							'name3'=>'scate','options3'=>'scates','title3'=>'三级分类',
							),
						)
					),
					array('type'=>'radio','name'=>'goodtype','options'=>'goodtype','title'=>'商品使用限制'),
					array('type'=>'array','show'=>array('goodtype'=>1),'name'=>'setgoodsids','title'=>'选择商品','add_tip'=>'添加一个商品',
						'list'=>array(
							array('type'=>'select','name'=>'pcate','options'=>'allgroups','title'=>'一级分类'),
							array('type'=>'select','name'=>'ccate','options'=>'allgroups','title'=>'二级分类'),
							array('type'=>'select','name'=>'goodsid','options'=>'goods','title'=>'商品名称'),
						),
						'details'=>array(
							array('type'=>'select3_line','name'=>'pcate','options'=>'pcategoods','title'=>'选择分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'二级分类',
							'name3'=>'goodsid','options3'=>'goods','title3'=>'请选择商品',
							),
						)
					),
					array('type'=>'radio','name'=>'islimitlevel','options'=>'islimitlevel','title'=>'会员使用限制'),
					array('type'=>'checkbox','name'=>'memberlevels','options'=>'member','title'=>'选择会员等级','show'=>array('islimitlevel'=>1)),
			);
	}else{
		$htmls['edit']=array(
					array('type'=>'switch','name'=>'status','title'=>'是否开启'),
					array('type'=>'string','name'=>'title','title'=>'优惠标题'),
					array('type'=>'radio','name'=>'type','title'=>'优惠类型','options'=>'huntypes'),
					array('type'=>'unitstring','unit'=>'元','name'=>'manfee','title'=>'满额条件'),
					array('type'=>'unitstring','unit'=>'折','name'=>'manzhe','title'=>'满额折扣','help'=>'未设置独立折扣的，统一按这里的折扣，填10不打折。商品折扣>分类折扣>统一折扣'),
					array('type'=>'radio','name'=>'catetype','options'=>'huncatetype','title'=>'分类独立折扣'),	
					array('type'=>'array','show'=>array('catetype'=>array(1,2)),'name'=>'setgroupids','title'=>'选择商品分类','add_tip'=>'添加一个分类',
						'list'=>array(
							array('type'=>'select','name'=>'pcate','options'=>'allgroups','title'=>'一级分类'),
							array('type'=>'select','name'=>'ccate','options'=>'allgroups','title'=>'二级分类'),
							array('type'=>'unitstring','unit'=>'折','name'=>'manzhe','title'=>'满额折扣'),
						),
						'details'=>array(
							array('type'=>'select2_line','name'=>'pcate','options'=>'pcates','title'=>'选择分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'二级分类',
							'name3'=>'scate','options3'=>'scates','title3'=>'三级分类',
							),
							array('type'=>'unitstring','unit'=>'折','name'=>'manzhe','title'=>'满额折扣'),
						)
					),
					array('type'=>'radio','name'=>'goodtype','options'=>'hungoodtype','title'=>'商品独立折扣'),
					array('type'=>'array','show'=>array('goodtype'=>array(1,2)),'name'=>'setgoodsids','title'=>'选择商品','add_tip'=>'添加一个商品',
						'list'=>array(
							array('type'=>'select','name'=>'pcate','options'=>'allgroups','title'=>'一级分类'),
							array('type'=>'select','name'=>'ccate','options'=>'allgroups','title'=>'二级分类'),
							array('type'=>'select','name'=>'goodsid','options'=>'goods','title'=>'商品名称'),
							array('type'=>'unitstring','unit'=>'折','name'=>'manzhe','title'=>'满额折扣'),
						),
						'details'=>array(
							array('type'=>'select3_line','name'=>'pcate','options'=>'pcategoods','title'=>'选择分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'二级分类',
							'name3'=>'goodsid','options3'=>'goods','title3'=>'请选择商品',
							),
							array('type'=>'unitstring','unit'=>'折','name'=>'manzhe','title'=>'满额折扣'),
						)
					),
					array('type'=>'radio','name'=>'islimitlevel','options'=>'islimitlevel','title'=>'会员使用限制'),
					array('type'=>'checkbox','name'=>'memberlevels','options'=>'member','title'=>'选择会员等级','show'=>array('islimitlevel'=>1)),
			);
	}
	$return['htmls']=$htmls;
	//获取等级
	$member=pdo_getall('jy_weishop_member_level',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'),'','id desc');
	foreach($member as &$v){
	    $id=$v['id'];
	    $return['member'][$id]=$v['title'];
	}
	$return['member'][0]='默认等级';
	//获取商品
	$goodsspram=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	$return['goods']=pdo_getall('jy_weishop_goods',$goodsspram,array('id','title'),'id');
	//获取分类
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'onepluginname OR'=>'onepluginname=\'jy_merch\' or onepluginname=\'\' ','uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'parentid'=>0);
	$return['pcates']=$return['pcategoods']=pdo_getall('jy_weishop_goods_group',$groupspram,array('title','id'),'id');
	foreach($return['pcates'] as &$pv){
			$pv['ccates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$pv['id']),array('title','id'),'id');
			foreach($pv['ccates'] as &$cv){
				$cv['scates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$cv['id']),array('title','id'),'id');
			}
	}
	foreach($return['pcategoods'] as &$gv){
			$gv['ccates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$gv['id']),array('title','id'),'id');
			$gv['ccates'][0]=array('id'=>0,'title'=>'默认分类');
			$gv['ccates'][0]['goods']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'ccate2'=>0,'pcate2'=>$gv['id'],'merchid'=>$merchid),array('title','id'),'id');
			foreach($gv['ccates'] as &$gcv){
				$gcv['goods']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'ccate2'=>$gcv['id'],'pcate2'=>$gv['id'],'merchid'=>$merchid),array('title','id'),'id');
			}
	}
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'onepluginname OR'=>'onepluginname=\'jy_merch\' or onepluginname=\'\' ','uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	$return['allgroups']=pdo_getall('jy_weishop_goods_group',$groupspram,array('title','id'),'id');
	$return['allgroups'][0]=array('id'=>0,'title'=>'默认分类');
	exi($return);
}
if($_RQ['op']=='postfee'){
	$saleset=$this->plugin_setting('saleset'.$merchid);
	$post=get_postdata();
	$groupids=';';
	$setgroupids=$post['setgroupids'];
	foreach($setgroupids as $sv){
		$cid=$sv['ccate']?:$sv['pcate'];
		if(strpos($groupids,';'.$cid.';') === false){
			$groupids .=$cid.';';
		}
	}
	$post['groupids']=$groupids;
	$goodids=';';
	$setgoodsids=$post['setgoodsids'];
	foreach($setgoodsids as $sv){
		$goodsid=$sv['goodsid'];
		if(strpos($goodids,';'.$goodsid.';') === false){
			$goodids .=$goodsid.';';
		}
	}
	$post['goodids']=$goodids;
	if($post['id']){
		$key=$post['id']-1;
		$saleset['enough_feeset'][$key]=$post;
	}else{
		$saleset['enough_feeset'][]=$post;
	}
	$this->plugin_setting('saleset'.$merchid,$saleset);
	exi('设置成功','','feelist');
}
if($_RQ['op']=='postnum'){
	$saleset=$this->plugin_setting('saleset'.$merchid);
	$post=get_postdata();
	$groupids=';';
	$setgroupids=$post['setgroupids'];
	foreach($setgroupids as $sv){
		$cid=$sv['ccate']?:$sv['pcate'];
		if(strpos($groupids,';'.$cid.';') === false){
			$groupids .=$cid.';';
		}
	}
	$post['groupids']=$groupids;
	$goodids=';';
	$setgoodsids=$post['setgoodsids'];
	foreach($setgoodsids as $sv){
		$goodsid=$sv['goodsid'];
		if(strpos($goodids,';'.$goodsid.';') === false){
			$goodids .=$goodsid.';';
		}
	}
	$post['goodids']=$goodids;
	if($post['id']){
		$key=$post['id']-1;
		$saleset['enough_numset'][$key]=$post;
	}else{
		$saleset['enough_numset'][]=$post;
	}
	$this->plugin_setting('saleset'.$merchid,$saleset);
	exi('设置成功','','numlist');
}
if($_RQ['op']=='posthun'){
	$saleset=$this->plugin_setting('saleset'.$merchid);
	$post=get_postdata();
	$groupids=';';
	$setgroupids=$post['setgroupids'];
	foreach($setgroupids as $sv){
		$cid=$sv['ccate']?:$sv['pcate'];
		if(strpos($groupids,';'.$cid.';') === false){
			$groupids .=$cid.';';
		}
	}
	$post['groupids']=$groupids;
	$goodids=';';
	$setgoodsids=$post['setgoodsids'];
	foreach($setgoodsids as $sv){
		$goodsid=$sv['goodsid'];
		if(strpos($goodids,';'.$goodsid.';') === false){
			$goodids .=$goodsid.';';
		}
	}
	$post['goodids']=$goodids;
	if($post['id']){
		$key=$post['id']-1;
		$saleset['enough_hunset'][$key]=$post;
	}else{
		$saleset['enough_hunset'][]=$post;
	}
	$this->plugin_setting('saleset'.$merchid,$saleset);
	exi('设置成功','','hunlist');
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$this->plugin_setting('saleset'.$merchid,$post);
	exi('设置成功');
}
if($_RQ['op']=='delfee' || $_RQ['op']=='delnum' || $_RQ['op']=='delhun'){
	$saleset=$this->plugin_setting('saleset'.$merchid);
	if($_RQ['op']=='delnum'){
		$set=$saleset['enough_numset'];
	}elseif($_RQ['op']=='delfee'){
		$set=$saleset['enough_feeset'];
	}elseif($_RQ['op']=='delhun'){
		$set=$saleset['enough_hunset'];
	}
	foreach($set as $key=>&$lv){
		$lv['id']=$lv['id']?:$key+1;
	}
	$id=intval($_RQ['id']);
	if($id){
		$k=$id-1;
		foreach($set as $k2=>$sv){
			if($sv['id']==$id || $k==$k2){
				unset($set[$k2]);
			}
		}
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$k=$id-1;
				foreach($set as $k2=>$sv){
					if($sv['id']==$id || $k==$k2){
						unset($set[$k2]);
					}
				}
			}
		}
	}
	if($_RQ['op']=='delnum'){
		$saleset['enough_numset']=$set;
	}elseif($_RQ['op']=='delfee'){
		$saleset['enough_feeset']=$set;
	}elseif($_RQ['op']=='delhun'){
		$saleset['enough_hunset']=$set;
	}
	$this->plugin_setting('saleset'.$merchid,$saleset);
	exi('删除成功！');
}
?>